package com.tjsinfo.tjsoa.workplan.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.tjsinfo.tjsoa.common.PageController;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.User;
import com.tjsinfo.tjsoa.workplan.dao.IWorkPlanMainDao;
import com.tjsinfo.tjsoa.workplan.vo.WorkPlanMain;

public class WorkPlanlistmodeAction extends Action{
	private IWorkPlanMainDao workPlanMainDao;

	public IWorkPlanMainDao getWorkPlanMainDao() {
		return workPlanMainDao;
	}

	public void setWorkPlanMainDao(IWorkPlanMainDao workPlanMainDao) {
		this.workPlanMainDao = workPlanMainDao;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		User tjsUser = (User) session.getAttribute("user");
		WorkPlanMain workPlanMain = new WorkPlanMain();
		TSystemUser user = new TSystemUser();
		user.setUserId(tjsUser.getUser_id());
		workPlanMain.setTjsUser(user);
		workPlanMain.setType(request.getParameter("type"));
		List list = workPlanMainDao.selectWorkPlanMainUserAndType(workPlanMain);
		Integer contentspage = null;
		String start = null;
		 PageController pageController = new PageController();
		    if(request.getParameter("page") != null && request.getParameter("start") == null )
			{
				pageController.setPageController(list);
				pageController.setMaxPageRows(18);
				list = (ArrayList) pageController.getPageContents(Integer.parseInt(request.getParameter("page") ));
		
			}
		    else if (request.getParameter("page") != null) {
				contentspage = Integer.parseInt(request.getParameter("page"));
				start = request.getParameter("start");
				pageController.setPageController(list);
				pageController.setMaxPageRows(18);
				if (start.equals("up")) {
					list = (ArrayList) pageController
							.getPageContents(contentspage - 1);
				} else if(start.equals("down")) {
					list = (ArrayList) pageController
							.getPageContents(contentspage + 1);
				}
				else if(start.equals("first"))
				{
					list = (ArrayList) pageController
					.getPageContents(1);
				}
				else if(start.equals("last"))
				{
					list = (ArrayList) pageController
					.getPageContents(pageController.getPageCount());
				}
			} else {
				pageController.setPageController(list);
				pageController.setMaxPageRows(18);
				list = (ArrayList) pageController.getPageContents(1);
			}
		request.setAttribute("list", list);
		request.setAttribute("type",request.getParameter("type"));
		request.setAttribute("pageController",pageController);
		return mapping.findForward("ok");
	}
	
}
